/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ordering.utils;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 *
 * @author sulan
 */
public class ConfigIni {

    private static Properties ini = null;

    static {
        try {
            ini = new Properties();
            ini.load(new FileInputStream("Config.ini"));
        } catch (Exception ex) {
            System.out.println("Load CONFIG.INI is false!!");
        }//End try
    }

    private ConfigIni() {
    }

    public static String getIniKey(String k) {
        if (!ini.containsKey(k)) {		//是否有 k 这个键
            System.out.println("The [ " + k + " ] Key is not exist!!");
            return "";
        }//End if(!ini.containsKey (k))
        return ini.get(k).toString();
    }

    public static void setIniKey(String k, String v) {
        if (!ini.containsKey(k)) {		//是否有 k 这个键
            System.out.println("The [ " + k + " ] Key is not exist!!");
            return;
        }//End if(!ini.containsKey (k))
        ini.put(k, v);
    }

    public static void saveIni(String k[]) {
        try {
            FileWriter fw = new FileWriter("Config.ini");
            BufferedWriter bw = new BufferedWriter(fw);
            //循环变量i是k字符串数组的下标
            for (int i = 0; i < k.length; i++) {
                bw.write(k[i] + "=" + getIniKey(k[i]));
                bw.newLine();
            }//End for
            bw.close();
            fw.close();
        } catch (Exception ex) {
            System.out.println("Save CONFIG.INI is false.");
        }//End try
    }

    public static Map getProperties()
    {
        Map configProperties = new HashMap();
        configProperties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        configProperties.put("hibernate.connection.username", ConfigIni.getIniKey("UserID"));
        configProperties.put("hibernate.connection.password", ConfigIni.getIniKey("Password"));
        configProperties.put("hibernate.connection.url", "jdbc:mysql://"+ConfigIni.getIniKey("IP")+":"+ConfigIni.getIniKey("Port")+"/"+ConfigIni.getIniKey("DBFname"));
        configProperties.put("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
        return configProperties;
    }
}
